package com.cy.wenxinapi.config;


import com.cy.wenxinapi.shiro.ThreadLocalToken;
import com.cy.wenxinapi.util.R;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TokenAspect {
    @Autowired
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * com.cy.wenxinapi.controller.*.*(..)) && !within(com.cy.wenxinapi.controller.ChatController)")
    public void aspect() {

    }

    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object proceed = point.proceed();
        try {
            if (proceed instanceof R) {
                R r = (R) proceed;
                String token = threadLocalToken.getToken();
                if (token != null) {
                    r.put("token", token);
                    threadLocalToken.clear();
                }
                return r;
            }
        } catch (Exception e) {
            System.out.println("异常");
        }

        return proceed;
    }
}
